package com.sharemarking.wa.platform.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharemarking.wa.common.Page;
import com.sharemarking.wa.common.ResponseParams;
import com.sharemarking.wa.common.entity.DriverVerify;
import com.sharemarking.wa.common.entity.Member;
import com.sharemarking.wa.common.entity.OrgVerify;
import com.sharemarking.wa.common.exception.DriverException;
import com.sharemarking.wa.common.exception.RequestException;
import com.sharemarking.wa.platform.dto.company.CarListDto;
import com.sharemarking.wa.platform.dto.company.CompanyDto;
import com.sharemarking.wa.platform.dto.company.CompanyInfoDto;
import com.sharemarking.wa.platform.dto.company.DriverListDto;
import com.sharemarking.wa.platform.mapper.CompanyMapper;
import com.sharemarking.wa.platform.mapper.DriverVerifyMapper;
import com.sharemarking.wa.platform.mapper.MemberMapper;
import com.sharemarking.wa.platform.mapper.OrgVerifyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.sharemarking.wa.common.SysHttpStatus.UN_DRIVER_VERIFY;
import static com.sharemarking.wa.common.SysHttpStatus.UN_VERIFY;

/**
 * @author dxy
 */
@Service
public class CompanyService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    private OrgVerifyMapper orgVerifyMapper;
    @Autowired
    private MemberMapper mm;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private DriverVerifyMapper driverVerifyMapper;

    public ResponseParams<?> getInfo() throws Exception{
        Integer memberId = ss.getCurrentUserId();
        Member member = mm.get(memberId);
        Integer role = memberMapper.getRoleByMemberId(memberId);
        if (role != null && !role.equals(1)&& !role.equals(2)) {
            throw RequestException.create(ResponseParams.isDataError("无权限"));
        }
        //判断是否已经认证))
        DriverVerify driverVerify = driverVerifyMapper.selectOne(new QueryWrapper<DriverVerify>().eq("member_id", memberId).eq("exam_state", 1));
        OrgVerify orgVerify = orgVerifyMapper.selectOne(new QueryWrapper<OrgVerify>().eq("member_id", memberId).eq("exam_state", 1));
        if (ObjectUtils.isEmpty(driverVerify) && ObjectUtils.isEmpty(orgVerify)) {
            throw DriverException.create(ResponseParams.error(UN_VERIFY));
        }
        CompanyDto company = new CompanyDto();
        if (member.getRole().equals(0) || member.getRole().equals(1)) {
            company = companyMapper.getCompany(memberId);
        }else if (member.getRole().equals(2)){
            company = companyMapper.getDriverCompany(memberId);
        }
        return ResponseParams.ok(company);
    }


    public ResponseParams<?> getCarList() throws Exception{
        Integer memberId = ss.getCurrentUserId();
        Integer role = memberMapper.getRoleByMemberId(memberId);
        if (role != null && !role.equals(2)) {
            throw RequestException.create(ResponseParams.isDataError("无权限"));
        }

        int pageIndex = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageIndex")) ? "1" : request.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageSize")) ? "10" : request.getParameter("pageSize"));

        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex", (pageIndex - 1) * pageSize);
        params.put("pageSize", pageSize);
        params.put("memberId", memberId);

        List<CarListDto> list = companyMapper.getCarList(params);

        return ResponseParams.ok(new Page<>(list, pageIndex, pageSize, companyMapper.getCarListCount(params), list.size() == pageSize));

    }



    public ResponseParams<?> getDriverList() throws Exception{
        Integer memberId = ss.getCurrentUserId();
        Integer role = memberMapper.getRoleByMemberId(memberId);
        if (role != null && !role.equals(2)) {
            throw RequestException.create(ResponseParams.isDataError("无权限"));
        }

        int pageIndex = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageIndex")) ? "1" : request.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageSize")) ? "10" : request.getParameter("pageSize"));

        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex", (pageIndex - 1) * pageSize);
        params.put("pageSize", pageSize);
        params.put("memberId", memberId);

        List<DriverListDto> list = companyMapper.getDriverList(params);

        return ResponseParams.ok(new Page<>(list, pageIndex, pageSize, companyMapper.getDriverListCount(params), list.size() == pageSize));

    }

}
